package com.wisely.springbootjpa.repository;

import com.wisely.springbootjpa.domain.User;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserRepository extends JpaRepository<User, Long> {

    //使用命名参数
    @Query(value = "from User u where u.name=:name")
    List<User> findByName1(@Param("name") String name);

    //使用参数索引
    @Query(value = "select * from user u where u.name=?1", nativeQuery = true)
    List<User> findByName2(String name);

    List<User> findByName(String name);

    List<User> findByName(String name, Sort sort);

    List<User> findByNameLike(String name);

    List<User> findByNameAndAddress(String name, String address);

    List<User> findFirst10ByName(String name);

    List<User> findTop30ByName(String name);

    @Modifying
    @Transactional
    @Query(value = "update User u set u.name = ?1 where id = ?2")
    int setName(String name, long id);

    List<User> findByNamePage(String name, PageRequest pageRequest);

}
